import urllib.parse  
import urllib.request
from urllib.error import URLError,HTTPError
from html.parser import HTMLParser
from bs4 import BeautifulSoup
import re
import os

directory = "D:\\xiaoshuo"

def mkdir():
   if os.path.exists(directory) == False:
      os.makedirs(directory)

def mkbookdir(bookname):
   dir = directory + "\\" +bookname     
   if os.path.exists(dir) == False:
      os.makedirs(dir)

def get(num):
    try:  
        url  =  "http://www.shuqi6.com/%s/" % num
        data = urllib.request.urlopen(url).read() 
        strdata = str(data, encoding="gbk", errors='ignore')
        return strdata

    except HTTPError as e:  
        print('Error code:',e.code)   
    except URLError as e:  
        print('Reason',e.reason)

def content(num,page):
    try:  
        url  =  "http://www.shuqi6.com/%s/%s/" % (num,page)
        data = urllib.request.urlopen(url).read() 
        strdata = str(data, encoding="gbk", errors='ignore')
        return strdata

    except HTTPError as e:  
        print('出错啦：',e.code)   
    except URLError as e:  
        print('错误：',e.reason)

def downContent(num,page):
    pagedata = content(num,page)
    soup = BeautifulSoup(pagedata,"html.parser")
    for a in soup.find_all("div",id="booktext",limit=1):
        text = a.get_text()
        return text;        

def downbook(booknum):
    mkdir()
    print("正在解析章节")
    pagedata = get(booknum)
    print("解析完毕")
    soup = BeautifulSoup(pagedata,"html.parser")
    
    bookname = soup.title.string.split('-')[0]
    bookname = bookname.strip()
    mkbookdir(bookname)

    ordernum = 0
    for a in soup.find_all(href=re.compile("^\d+$")):
        ordernum += 1
        page = a.get('href')
        title = a.get('title')
        print("正在下载: %s " % title)
        
        rabish = ["/","\\","*","?","<",">","|",":"]
        for x in rabish:
            title = title.replace(x,' ')

        filename = "%s\\%s\\%d.%s.txt" % (directory,bookname,ordernum,title)
        fo = open(filename,"w",encoding='utf-8')
        booktext = downContent(booknum,page)
        fo.write(booktext)
    print("下载完成，共下载 %d 章" % ordernum)